package com.seu.project.seuquiz.controller;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seu.project.seuquiz.annotation.AuthCheck;
import com.seu.project.seuquiz.common.BaseResponse;
import com.seu.project.seuquiz.common.DeleteRequest;
import com.seu.project.seuquiz.common.ErrorCode;
import com.seu.project.seuquiz.common.ResultUtils;
import com.seu.project.seuquiz.exception.BusinessException;
import com.seu.project.seuquiz.model.converter.CollegeContestMapConverter;
import com.seu.project.seuquiz.model.dto.collegeContestMap.CollegeContestMapAddRequest;
import com.seu.project.seuquiz.model.dto.collegeContestMap.CollegeContestMapQueryRequest;
import com.seu.project.seuquiz.model.dto.collegeContestMap.CollegeContestMapUpdateRequest;
import com.seu.project.seuquiz.model.entity.CollegeContestMap;
import com.seu.project.seuquiz.model.entity.UserScoreMap;
import com.seu.project.seuquiz.service.CollegeContestMapService;
import com.seu.project.seuquiz.util.EasyExcelUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 学院-竞赛信息接口
 * @author SoftPro
 * @createDate: 2023-07-16 09:43
 */
@RestController
@RequestMapping("/college")
@Api(tags = "学院竞赛管理接口")
@Slf4j
public class CollegeContestMapController {
    // 实现根据学院、竞赛的增删改查

    @Resource
    private CollegeContestMapService collegeContestMapService;

    // region_start  基本增删改查

    /**
     * 新增题目
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Long> addCollegeContestMap(@RequestBody CollegeContestMapAddRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        String collegeName = request.getCollegeName();
        String contestId = request.getContestId();
        Integer collegeStudentTotal = request.getCollegeStudentTotal();
        Integer joinTotal = request.getJoinTotal();
        Integer totalScore = request.getTotalScore();
        
        
        long id = collegeContestMapService.collegeContestMapAdd(collegeName, contestId, collegeStudentTotal, joinTotal, totalScore);
        return ResultUtils.success(id);
    }

    /**
     * 删除题目
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> deleteCollegeContestMapById(@RequestBody DeleteRequest deleteRequest){
        if(deleteRequest==null || deleteRequest.getId()<0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = collegeContestMapService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 批量删除
     * @param deleteRequest
     * @return
     */
    @PostMapping("/deleteByIds")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> deleteCollegeContestMapByIds(@RequestBody DeleteRequest deleteRequest){
        if(deleteRequest==null || deleteRequest.getIds().size() < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = collegeContestMapService.removeByIds(deleteRequest.getIds());
        return ResultUtils.success(b);
    }

    /**
     *
     * @param updateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> updateCollegeContestMap(@RequestBody CollegeContestMapUpdateRequest updateRequest){
        if(updateRequest==null || updateRequest.getId()<0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = collegeContestMapService.updateById(CollegeContestMapConverter.collegeContestMapUpdateRequestToCollegeContestMap(updateRequest));
        return ResultUtils.success(b);
    }

    /**
     * 根据id查询题目
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<CollegeContestMap> getCollegeContestMapById(int id){
        if(id<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        CollegeContestMap collegeCollegeContestMap = collegeContestMapService.getById(id);
        return ResultUtils.success(collegeCollegeContestMap);
    }

    /**
     * 根据查询条件获取全部的题目信息  todo: 待测试
     * @param request
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<CollegeContestMap>> listCollegeContestMap(CollegeContestMapQueryRequest request) {
        QueryWrapper<CollegeContestMap> queryWrapper = collegeContestMapService.getQueryWrapper(request);
        List<CollegeContestMap> list = collegeContestMapService.list(queryWrapper);
        return ResultUtils.success(list);
    }

    /**
     * 分页获取题目     todo:待测试
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<CollegeContestMap>> listCollegeContestMapByPage(CollegeContestMapQueryRequest request) {
        long current = 1;
        long size = 10;
        if (request != null) {
            current = request.getCurrent();
            size = request.getPageSize();
        }
        QueryWrapper<CollegeContestMap> queryWrapper = collegeContestMapService.getQueryWrapper(request);
        Page<CollegeContestMap> page = collegeContestMapService.page(new Page(current, size), queryWrapper);
        return ResultUtils.success(page);
    }

    // region_end

    /**
     * 以excel文件方式批量添加学院竞赛成绩
     * @param multipartFile
     * @return
     */
    @PostMapping("/add/batch")
    public BaseResponse<String> addCollegeContestBatch(@RequestPart("file") MultipartFile multipartFile){
        // 校验文件
        long size = multipartFile.getSize();
        String originalFilename = multipartFile.getOriginalFilename();
        // 校验文件大小
        final long ONE_MB = 1024 * 1024L;
        if (size > ONE_MB) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件超过 1M");
        }
        // 校验文件后缀   aaa.png
        String suffix = FileUtil.getSuffix(originalFilename);
        final List<String> validFileSuffixList = Arrays.asList("xlsx", "xls");
        if (!validFileSuffixList.contains(suffix)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件后缀非法");
        }

        List<CollegeContestMap> collegeContestMapList = EasyExcelUtils.excelToCollegeContestList(multipartFile);
        boolean saveBatch = collegeContestMapService.saveBatch(collegeContestMapList);
        if (!saveBatch) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "数据库错误");
        }
        return ResultUtils.success("添加完成");
    }

}
